/* @(#)GSBalanceBargainFileAction.java
 * 
 *  Project:GS
 *  
 *  Modify  Information:
 *  ===============================================
 *   Author          Date            Description
 *  -------------- ---------------  ---------------
 *  Jenny  Li      2006-5-22          新增
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 *
 */
package com.hitrust.gs.action;

import java.io.File;
import java.io.FileInputStream;

import javax.servlet.ServletOutputStream;

import com.hitrust.gs.bean.GSBargainBean;
import com.hitrust.gs.business.GSBargainProcessor;
import com.hitrust.gs.dao.GSBargainDAO;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.GSException;

/**
 * @author JennyLi
 *
 */
public class GSBalanceBargainFileAction extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {
	    FileInputStream tFileInput = null;
		GSBargainProcessor tBargainProc = new GSBargainProcessor();
		try{
//			tBargainProc.getBargainCont(aForm);
            String tBgnId = aForm.getRequest().getParameter("bgnId");
            GSBargainDAO tBargDAO = new GSBargainDAO();
            GSBargainBean tBargBean = tBargDAO.queryBargainById(tBgnId);
            String tFileName = tBargBean.getGs_bgn_filename();
            
             String tPath = GSSystemConfigurator.getParam("gs.upload.savepath");
             String tPathFile = tPath + File.separator + tFileName;
             File tFile = new File(tPathFile);
             if(!tFile.exists()){
                 GSLogWriter.error(this,"文件不存在");
                 throw new GSException("文件不存在");
             }else{
                tFileInput = new FileInputStream(tFile);
                long length = tFile.length();
                aForm.getResponse().setContentType("application/x-msdownload");
                aForm.getResponse().setContentLength((int)length);
                aForm.getResponse().setHeader("Content-Disposition",
                         "attachment;filename=" + GSBaseManager.toUtf8String(tFileName));
                 
                 byte[] tBt = new byte[1024];
/*               int tRs = tFileInput.read(tBt);
                 while(tRs != -1){
                     tOutputStream.write(tBt,0,tRs);
                     tRs = tFileInput.read(tBt);
                 }
*/
                int k = 0;
                while((long)k < length)
                {
                    int j = tFileInput.read(tBt, 0, 1024);
                    k += j;
                    //aForm.getResponse().getOutputStream().flush();
                    aForm.getResponse().getOutputStream().write(tBt, 0, j);
                }
                tFileInput.close();
                aForm.getResponse().getOutputStream().flush();
                aForm.getResponse().getOutputStream().close();
                GSLogWriter.debug("文件下载成功");
             }
		}catch(Exception e){
            GSLogWriter.error(this,e.toString());
			aForm.getRequest().setAttribute("window","true");
			throw e;
		}
		return GSBaseConstants.FORWARD_SUCCESS;
	}

}
